package com.xbongbong.crm.abstracts;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormDataHelp;
import com.xbongbong.paas.help.OwnerAndCoUserHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.parent.abstracts.AbstractBusinessDetailBaseInfoService;
import com.xbongbong.parent.explain.FormDataDetailFormExplainHandleName;
import com.xbongbong.parent.parse.data.FormDataDetailAnalysisDataHelp;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/02/12 13:18
 */
@Component
public abstract class AbstractCrmDetailBaseInfoService extends AbstractBusinessDetailBaseInfoService<PaasFormEntityExt, PaasFormDataEntityExt> {


    @Resource
    private FormDataHelp formDataHelp;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private OwnerAndCoUserHelp ownerAndCoUserHelp;
    @Resource
    private FormDataDetailFormExplainHandleName formDataDetailFormExplainHandleName;
    @Resource
    private FormDataDetailAnalysisDataHelp formDataDetailAnalysisDataHelp;
    @Resource
    private RichTextService richTextService;

    @Override
    public PaasFormDataEntityExt getData(Integer businessType, Integer subBusinessType, Integer saasMark, String corpid, Long dataId, Long formId) throws XbbException {
        PaasFormDataEntityExt formData = formDataHelp.getFormData(dataId, corpid, businessType);
        if (Objects.isNull(formData)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        FormDataUtil.checkData(formData, subBusinessType);
        //拼装富文本数据
        richTextService.addRichTextByDataId(dataId, corpid, formData.getFormId(), businessType, saasMark, formData.getDel(), formData.getData());
        return formData;
    }

    @Override
    public HandlerExplainVO handleExplain(List<FieldAttrEntity> explainList, PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        handlerExplainDTO.setExplainList(explainList);
        ownerAndCoUserHelp.getOwnerIdAndCoUserId(handlerExplainDTO, handlerExplainDTO.getSaasMark(), handlerExplainDTO.getBusinessType(), paasFormDataEntityExt, new HashMap<>(), DelEnum.NORMAL.getDel());
        handlerExplainDTO.setFormData(paasFormDataEntityExt.getData());
        HandlerExplainVO handlerExplainVO = formDataDetailFormExplainHandleName.handleExplain(explainList, formEntity, paasFormDataEntityExt, handlerExplainDTO);

        return handlerExplainVO;
    }

    @Override
    public PaasFormEntityExt getForm(UserVO loginUser, PaasFormDataEntityExt paasFormDataEntityExt, String corpid) throws XbbException {
        Long formId = paasFormDataEntityExt.getFormId();
        PaasFormEntityExt paasFormEntity = paasFormModel.getByKey(formId, corpid);
        if ( Objects.isNull(paasFormEntity) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018, MessageConstant.FORM_NOT_EXIST);
        }
        paasFormModel.validateByPermission(paasFormEntity, loginUser);
        return paasFormEntity;
    }


    @Override
    public List<FieldAttrEntity> getExplainList(PaasFormEntityExt form, String corpid) throws XbbException {
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(form.getId(), corpid);
        return JSON.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
    }

    @Override
    public void handleDetailSpecialExplain( PaasFormDataEntityExt formDataEntity, List<FieldAttrEntity> explainList) throws XbbException {

    }

    @Override
    public void analysisGetOfThousandBits(JSONObject data, List<FieldAttrEntity> explainList, Boolean analysisAmountOfThousandBitsWithOriginalValue) throws XbbException {
        // 千分位换算
        formDataDetailAnalysisDataHelp.analysisGetOfThousandBits(data, explainList, analysisAmountOfThousandBitsWithOriginalValue);
    }

    @Override
    public FormDataGetVO returnVo(PaasFormDataEntityExt formData, HandlerExplainVO handlerExplainVO, JSONObject data, PaasFormEntityExt form, String userId, String corpid) throws XbbException {
        FormDataGetVO formDataGetVO = new FormDataGetVO();
        formDataGetVO.setPaasFormEntity(form);
        formDataGetVO.setHeadList(ExplainUtil.getFieldListNoIncludeSubForm(handlerExplainVO).getHeadList());
        formDataGetVO.setData(data);
        return formDataGetVO;
    }

}
